"""
@File    :   introduction.py
@Time    :   2024/03/28 22:25:54
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   需要引入其他业务模块依赖的依赖
"""

from typing import Annotated

from fastapi import Body, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.client.schemas.out import (
    Oauth2ClientCompleteOutSchema,
)
from src.business.admin.system.oauth2.client.service import Oauth2ClientService
from src.business.admin.system.tenant.tenant.dependencies.no_introduction import (
    ValidateTenantExistsDependencies,
)
from src.business.admin.system.tenant.tenant.schemas.out import TenantCompleteOutSchema
from src.dependencies.no_introduction import depend_get_master_db_session

from .no_introduction import GetOAuth2ClientIDDependencies


class ValidateOAuth2ClientExistsDependencies:
    """验证OAuth2客户端是否存在的依赖类"""

    @staticmethod
    async def from_body(
        tenant: Annotated[
            TenantCompleteOutSchema, Depends(ValidateTenantExistsDependencies.from_body)
        ],
        client_id: Annotated[str, Depends(GetOAuth2ClientIDDependencies.from_body)],
        db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
    ) -> Oauth2ClientCompleteOutSchema:
        oauth2_client: Oauth2ClientCompleteOutSchema = (
            await Oauth2ClientService.get_oauth2_client_use_client_id(
                db_session, client_id, tenant.id
            )
        )
        return oauth2_client

    @staticmethod
    async def from_header(
        tenant: Annotated[
            TenantCompleteOutSchema,
            Depends(ValidateTenantExistsDependencies.from_header),
        ],
        client_id: Annotated[str, Depends(GetOAuth2ClientIDDependencies.from_body)],
        db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
    ) -> Oauth2ClientCompleteOutSchema:
        oauth2_client: Oauth2ClientCompleteOutSchema = (
            await Oauth2ClientService.get_oauth2_client_use_client_id(
                db_session, client_id, tenant.id
            )
        )
        return oauth2_client

    @staticmethod
    async def from_query(
        tenant: Annotated[
            TenantCompleteOutSchema,
            Depends(ValidateTenantExistsDependencies.from_query),
        ],
        client_id: Annotated[str, Depends(GetOAuth2ClientIDDependencies.from_query)],
        db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
    ) -> Oauth2ClientCompleteOutSchema:
        oauth2_client: Oauth2ClientCompleteOutSchema = (
            await Oauth2ClientService.get_oauth2_client_use_client_id(
                db_session, client_id, tenant.id
            )
        )
        return oauth2_client


class ValidateOAuth2ClientSecretDependencies:
    """验证OAuth2客户端密钥是否正确的依赖类"""

    @staticmethod
    async def from_body(
        oauth2_client: Annotated[
            Oauth2ClientCompleteOutSchema,
            Depends(ValidateOAuth2ClientExistsDependencies.from_body),
        ],
        client_secret: str = Body(
            ..., min_length=1, max_length=255, description="客户端密钥"
        ),
    ) -> Oauth2ClientCompleteOutSchema:
        # 验证OAuth2客户端的密钥是否正确
        Oauth2ClientService.validate_oauth2_client_secret(
            oauth2_client.client_secret, client_secret
        )
        return oauth2_client
